import sun.nio.ch.ThreadPool;

import java.util.concurrent.*;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;

public class Threadpool {
    public static void main(String[] args) {
        int corePoolSize = 2;
        int maximumPoolSize = 10;
        int keepAliveTime = 60;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = (BlockingQueue<Runnable>) new ArrayBlockingQueue(1);
        ThreadFactory threadFactory = new ThreadFactory() {
            private String prefix = "thread...";
            private AtomicInteger atomicInteger = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(prefix+atomicInteger.incrementAndGet());
            }
        };
        RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                handler);
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                String tName = Thread.currentThread().getName();
                System.out.println(tName);
                try{Thread.sleep(5000);}catch (Exception e){}
            }
        });
    }
}
